package com.googlecode.grs.controller;

import com.googlecode.grs.core.controller.EventController;
import com.googlecode.grs.mechanism.CenteringRangeFinderArray;
import com.googlecode.grs.mechanism.CenteringRangeFinderListener;
import com.googlecode.grs.mechanism.CenteringWall;
import com.googlecode.grs.mechanism.CenteringWallDriveMechanism;

/**
 * Finds the furthest wall wrt the robot and tells the robot to turn towards
 * that wall Notified when furthest wall changes and reacts accordingly
 * 
 * @author Dan
 * 
 */

public class WallCenteringController extends EventController implements
		CenteringRangeFinderListener {

	CenteringRangeFinderArray sensor;
	CenteringWallDriveMechanism driver;

	/**
	 * 
	 * @param furthestWall
	 */
	public WallCenteringController(CenteringWallDriveMechanism driver,
			CenteringRangeFinderArray sensor) {
		this.sensor = sensor;
		this.driver = driver;
	}

	/**
	 * Starts this controller, which requires that the DriveMechanism be started
	 */
	public void start() {
		driver.start();
		sensor.startListening();
		super.start();
	}

	@Override
	public void startListening() {
		sensor.addCenteringRangeFinderListener(this);

	}

	@Override
	public void stopListening() {
		sensor.removeCenteringRangeFinderListner(this);

	}

	@Override
	public void halt() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void unPause() {
		// TODO Auto-generated method stub

	}

	/**
	 * On the event that the furthest wall changes, the robot is told to move
	 * towards the new furthest wall at the time
	 */
	@Override
	public void furthestWallChanged(CenteringWall newFurthest) {
		log("turning!");
		driver.turnTo(newFurthest);
	}

}
